//
//  HorizontalScroller.m
//  Rong360
//
//  Created by tianliwei on 7/2/14.
//  Copyright (c) 2014 tianliwei. All rights reserved.
//

#import "HorizontalScroller.h"
#import <QuartzCore/QuartzCore.h>
@implementation HorizontalScroller

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setup];
    }
    return self;
}

- (void)setButtonData:(NSArray *)data buttonGap:(NSInteger)buttonGap buttonWidth:(NSInteger)buttonWidth buttonHeight:(NSInteger)buttonHeight maskWidth:(NSInteger)maskWidth{
    self.buttonData = data;
    self.buttonGap = buttonGap;
    self.buttonHeight = buttonHeight;
    self.buttonWidth = buttonWidth;
    self.maskWidth = maskWidth;
    [self setup];
}

- (void)setup{
    self.delegate = self;
    self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    self.layer.borderWidth = .5;
    self.indicatorStyle = UIScrollViewIndicatorStyleDefault;
    self.scrollEnabled = YES;
    [self setBackgroundColor:[UIColor clearColor]];
    [self setCanCancelContentTouches:NO];
    [self setShowsHorizontalScrollIndicator:NO];
    [self setShowsVerticalScrollIndicator:NO];
    [self setContentSize:CGSizeMake((self.buttonData.count * (self.buttonGap + self.buttonWidth)), [self bounds].size.height)];
    //[self.layer setCornerRadius:5.0f];
    
    // Add gradient mask to the right side of buttonScrollView
    self.maskLayer = [CAGradientLayer layer];
    id outerColor = (id)[[UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:1.0] CGColor];
    id innerColor = (id)[[UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:0.0] CGColor];
    
    self.maskLayer.frame = CGRectMake(self.frame.size.width - self.maskWidth, 0, self.maskWidth, self.frame.size.height);
    self.maskLayer.startPoint = CGPointMake(1.0, 0.5);
    self.maskLayer.endPoint = CGPointMake(0, 0.5);
    self.maskLayer.colors = @[outerColor, innerColor];
    
    // Add buttons to the scrollview
    NSInteger scrollX = 0;
    
    for (int i = 0; i < self.buttonData.count; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        button.frame = CGRectMake(scrollX, 0, self.buttonWidth, self.buttonHeight);
        [button setBackgroundColor:RGBCOLOR(242, 242, 242)];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:APPCOLOR forState:UIControlStateSelected];
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:self.buttonData[i] forState:UIControlStateNormal];
        [button setTag:i];
        [button.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
        //[button.layer setCornerRadius:3.0f];
        
        [self addSubview:button];
        
        scrollX += (self.buttonGap+self.buttonWidth);
    }
    
    [self.layer insertSublayer:self.maskLayer atIndex:self.buttonData.count];
}

- (void)buttonClicked:(UIButton *)sender{
    if (self.hsDelegate) {
        [self.hsDelegate horizontalScroller:self didSelectColumnAtIndex:sender.tag];
    }
}

#pragma mark UIScrollView Delegate Method

- (void)scrollViewDidScroll:(UIScrollView *)sv {
    // Keep the mask layer on the right side as the content is scrolled
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    self.maskLayer.position = CGPointMake(sv.contentOffset.x + sv.frame.size.width - (self.maskWidth / 2), sv.frame.size.height / 2);
    [CATransaction commit];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
